//
//  HYStatusCell.swift
//  iOSWeibo
//
//  Created by why on 16/7/27.
//  Copyright © 2016年 王浩宇. All rights reserved.
//

import UIKit
import SnapKit

class HYStatusCell: UITableViewCell {
    
    ///微博模型
    var status: HYStatus? {
        didSet {
            // 在cell拿到模型的时候,将模型赋值给originalView
            originalView.status = status
            
            // cell拿到模型将被转发微博模型传递给被转发的View
            retweetView.retweetStatus = status?.retweeted_status
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///设置UI界面
    private func setupUI() {
        
        self.contentView.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        //添加控件
        self.contentView.addSubview(originalView)
        self.contentView.addSubview(retweetView)
        self.contentView.addSubview(bottomView)
        
        //添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(self.contentView).offset(8)
            
            // 模拟原创view不固定的高度
//            let height = CGFloat(arc4random_uniform(60)) + 44
//            make.height.equalTo(height)
        }
        
        //被转发微博View
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        //底部view
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(retweetView.snp_bottom)
            make.height.equalTo(37)
        }
        
        //让cell的高度自己适应,起始是根据contentView的高度来
        //需要约束好contentView
        self.contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
        }
        
        
        //contentView的高度约束需要参照里面的内容来
        self.contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomView)
        }
        
    }
    
    //MARK: - 懒加载
    /// 原创view
    private lazy var originalView: HYStatusOriginalView = HYStatusOriginalView()
    
    /// 被转发微博View
    private lazy var retweetView: HYStatusRetweetView = HYStatusRetweetView()
    
    /// 底部view
    private lazy var bottomView: HYStatusBottomView = HYStatusBottomView()
}
